package vip.zcn.webmanagement.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import vip.zcn.webmanagement.pojo.Emp;
import vip.zcn.webmanagement.pojo.PageBean;
import vip.zcn.webmanagement.service.EmpService;
import vip.zcn.webmanagement.utils.Result;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/emps")
public class EmpController {

    @Autowired
    private EmpService empService;


    @GetMapping()
    public Result page(@RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                       String name, Short gender, @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,@DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
                       ) {
        PageBean pageBean = empService.page(page, pageSize,name,gender,begin,end);
        return Result.success(pageBean);
    }

    // 根据id查询员工
    @GetMapping("/{id}")
    public Result get(@PathVariable Integer id) {
        Emp emp = empService.getEmpById(id);
        return Result.success(emp);
    }

    // 批量删除员工
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        empService.delete(ids);
        return Result.success();
    }
    // 添加员工
    @PostMapping()
    public Result save(@RequestBody Emp emp) {
        empService.save(emp);
        return Result.success();
    }

    // 修改员工
    @PutMapping()
    public Result update(@RequestBody Emp emp) {

        empService.update(emp);
        return Result.success();
    }
}
